﻿using EasyCore.App;
using EasyCore.Extensions;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyAdmin.Web.Middlewares
{
    /// <summary>
    /// 网站状态中间件：主要用于设置网站的维护状态等
    /// </summary>
    public class WebStatusMiddleware
    {
        /// <summary>
        /// 委托
        /// </summary>
        private readonly RequestDelegate _next;

        public WebStatusMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        /// <summary>
        /// 调用方法
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task InvokeAsync(HttpContext context)
        {
            bool webStatus = EasyApp.Configuration.Get<bool>("WebSettings:WebStatus");
            if (!webStatus)
            {
                //站点处理关闭时，返回423
                context.Response.StatusCode = 423;
                return;
            }
            await _next(context);
            //var path = context.Request.Path.Value;//获取请求路径
            //                                      // 检查请求路径是否以 "/biz" 开头
            //if (path.Length > 1 && !path.Contains('.') && !path.StartsWith("/sys", StringComparison.OrdinalIgnoreCase))
            //{
            //    // 通过 context.RequestServices 解析
            //    var configService = context.RequestServices.GetService<IConfigService>();
            //    // 获取网站状态
            //    var webStatus = await configService.GetByConfigKey(CateGoryConst.CONFIG_SYS_BASE, SysConfigConst.SYS_WEB_STATUS);
            //    // 如果网站状态为禁用，则返回 443 状态码
            //    if (webStatus.ConfigValue == CommonStatusConst.DISABLED)
            //    {
            //        context.Response.StatusCode = 423;
            //        return;
            //    }
            //}
            //await _next(context);
        }


    }
}
